package com.protocols.sctp.m3ua;

import com.protocols.GeneralProtocolData;
import com.protocols.OctaveInteger;
import com.protocols.ProtoOperations;
import com.protocols.ProtocolStackException;

public class M3uaPayLoadData  implements GeneralProtocolData{

	private OctaveInteger M3ua_Version= new OctaveInteger(1,1);
	private OctaveInteger Reserved= new OctaveInteger(0,1);
	private OctaveInteger M3ua_Message_class= new OctaveInteger(1,1);	//ASP Transfer Message	
	private OctaveInteger M3ua_Message_type= new OctaveInteger(1, 1);	// Payload Data
	private OctaveInteger M3ua_MessageLength= new OctaveInteger(4);
	
	
	public NetworkAppearance NetworkAppearance = new NetworkAppearance(false);
	public RoutingContext RoutingContext = new RoutingContext(false);
	public ProtocolData ProtocolData = new ProtocolData();
	public CorrelationID CorrelationID = new CorrelationID(false);	

	public M3uaPayLoadData() {
	}
	
	public M3uaPayLoadData(int SI) {
		ProtocolData.setServiceIndicator(SI);
	}
	
	public byte [] getByteArray(){	
		int len=8+NetworkAppearance.getLength()+RoutingContext.getLength()+ProtocolData.getLength()+CorrelationID.getLength();
			
		M3ua_MessageLength.setValue(len);
		
		return  ProtoOperations.concatArrays(M3ua_Version.getByteArray(), Reserved.getByteArray(), M3ua_Message_class.getByteArray(), M3ua_Message_type.getByteArray(), M3ua_MessageLength.getByteArray(), NetworkAppearance.getByteArray(),RoutingContext.getByteArray(),ProtocolData.getByteArray(),CorrelationID.getByteArray());

	}

	public int getLength(){
		return getByteArray().length;
	}
	
	public int getM3ua_Version() {
		return M3ua_Version.Value;
	}


	public int getM3ua_Message_class() {
		return M3ua_Message_class.Value;
	}



	public int getM3ua_Message_type() {
		return M3ua_Message_type.Value;
	}



	public int getM3ua_MessageLength() {
		return M3ua_MessageLength.Value;
	}

	public byte [] decode(byte [] input) throws ProtocolStackException{
		
		if (input[0]==1 && input[2]==1 && input[3]==1){			
			
			M3ua_MessageLength.Value= ProtoOperations.intFromByteArray(input, 4, 4);
			
			// extract the m3ua body
			byte [] remainder = ProtoOperations.extractByteArray(input, 8,M3ua_MessageLength.Value-8 );
			
			remainder=NetworkAppearance.decode(remainder);
			remainder=RoutingContext.decode(remainder);
			
			remainder=ProtocolData.decode(remainder);
			byte [] payload=ProtoOperations.extractByteArray(remainder, 1, (remainder[0]&0xff));
			CorrelationID.decode(ProtoOperations.extractByteArray(remainder, (remainder[0]&0xff)+1, remainder.length-(remainder[0]&0xff)-1));
	
			return payload;
		}
		else
			throw new ProtocolStackException("Decode Failure:M3uaPayLoadData"+ProtoOperations.getHexString(input));
		
		
		
	}
	


	

}
